package com.jxwifi.cloud.quickclean.card;


import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.basicactivity.BasicActivity;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.CardRollBean;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.view.SuperSwipeRefreshLayout;

import org.simple.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CardRollActivity extends ClientBasicActivity implements CardRollView {

    @Bind(R.id.et_card_roll)
    EditText mEtCardRoll;
    @Bind(R.id.tv_card_roll_button)
    TextView mTvCardRollButton;
    @Bind(R.id.srl_card_roll_list)
    SuperSwipeRefreshLayout mSrlCardRollList;
    @Bind(R.id.rv_card_roll_list)
    RecyclerView mRvCardRollList;

    private CardRollPresenter mPresenter;

    private View notDataView;
    private String mParameters;//pay_come表示支付详情进来，main_come表示首页进入
    private String mServiceCode="";
    private String mMoney="";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_roll);
        ButterKnife.bind(this);
        openEventBus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        mParameters=getIntent().getStringExtra("parameters");
        if (StateUtils.PAY_COME.equals(mParameters)){
            mServiceCode=getIntent().getStringExtra("service_code");
            mMoney=getIntent().getStringExtra("money");
        }
        mPresenter=new CardRollPresenter(mBaseContext,this);
        initHead();
        initView();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("清洁券");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    public void initView(){
        mPresenter.getinitData();
        mPresenter.Refresh();
    }

    @OnClick({R.id.tv_card_roll_button})
    void clickCK(View v){
        switch (v.getId()){
            case R.id.tv_card_roll_button:
                mPresenter.getRollButton();
                break;
        }
    }

    @Override
    public void getonfishs(CardRollBean mBean) {
        EventBus.getDefault().post(mBean, "e-CardRoll");
        finish();
    }

    @Override
    public String getMoney() {
        return mMoney;
    }

    @Override
    public String getServiceCode() {
        return mServiceCode;
    }

    @Override
    public String getParameters() {
        return mParameters;
    }

    @Override
    public EditText getEtCardRoll() {
        return mEtCardRoll;
    }

    @Override
    public TextView getCardRollButton() {
        return mTvCardRollButton;
    }

    @Override
    public SuperSwipeRefreshLayout getSrlCardRollList() {
        return mSrlCardRollList;
    }

    @Override
    public RecyclerView getRvCardRollList() {
        return mRvCardRollList;
    }

    @Override
    public View getViews() {
        notDataView = getLayoutInflater().inflate(R.layout.null_layout, (ViewGroup) mRvCardRollList.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.Refresh();
            }
        });
        return notDataView;
    }

    @Override
    public void onData() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.getData();
        }
    }

    @Override
    public void onRollButton() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.getRollButton();
        }
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }


}
